from djcontrol import *

from Tkinter import *
import djcontrolutil

class DjOptionMenu(DjControl):
	"""
	Menu widget, based on Tkinter OptionMenu.
	The list of menuitems is specified along with a callback function, upon creation. 
	
	Properties:
	*currentItem* - Set the currently chosen item.
	"""
	
	# Note that only names beginning with '_', or the special case basecontrol, should be used.
	_autoProperties = None
	_variable = None
	basecontrol = 0
	
	def __init__(self, parent, astrMenuItems, defaultValue=None, fnCallback=None , **options):
		"""Constructs an OptionMenu. Use the currentItem property to get or set the value. If a callback function is provided, it will be called whenever the value is set. Items cannot be changed after construction."""
		parent = djcontrolutil.getbasecontrol(parent)
		self._variable = StringVar(parent)
		self.basecontrol = OptionMenu(parent, self._variable, *astrMenuItems)
		if defaultValue!=None: self._variable.set(defaultValue)
		
		# Call the callback whenever the variable changes.
		if fnCallback!=None:
			traceCallback = lambda *args: fnCallback(self._variable.get())
			self._variable.trace('w',traceCallback)
		
		self.set_many(**options)
	
	
	_customProperties = ('currentItem')
	def _lookupCustomProperties(self, bSet, strAttribute, value=None):
		if strAttribute=='currentItem':
			if bSet:
				self._variable.set(value)
			else:
				return self._variable.get()
